<?php

namespace App\Http\Controllers\Internal\System;

use App\Http\Controllers\BaseController;
use App\Models\System\SystemNotifyCenterModel;
use App\Service\System\SystemNotifyService;
use Illuminate\Http\Request;

class SystemNotifyController extends BaseController
{
    public function customerBatchPush(Request $request)
    {
        $this->validate($request, [
            'userIds'     => 'required|array',
            'userIds.*'   => 'required|int',
            'title'       => 'required|string|max:255',
            'summary'     => 'required|string|max:65536',
            'url'         => 'sometimes|string',
            'shopId'      => 'sometimes|int',
            'type'        => 'sometimes|int|in:' . implode(',', array_keys(SystemNotifyCenterModel::NOTIFY_TYPE_MAPPING)),
            'target'      => 'sometimes|int|in:' . implode(',', array_keys(SystemNotifyCenterModel::NOTIFY_TARGET_MAPPING)),
            'smsTemplate' => 'sometimes|string|max:255',
            'method'      => 'sometimes|int|in:' . implode(',', array_keys(SystemNotifyCenterModel::METHOD_TYPES)),
            'content'     => 'sometimes|string|max:65536',
            'isPop'       => 'sometimes|int|in:0,1',
            'popArea'     => 'sometimes|int',
            'logTitle'    => 'sometimes|string|max:255',
        ]);
        try {
            SystemNotifyService::customerBatchPush(
                $request->userIds,
                $request->title,
                $request->summary,
                (string)$request->url,
                (int)$request->shopId,
                (int)$request->post('type', SystemNotifyCenterModel::NOTIFY_TYPE_3),
                (int)$request->post('target', SystemNotifyCenterModel::NOTIFY_TARGET_1),
                (string)$request->smsTemplate,
                (int)$request->post('method', SystemNotifyCenterModel::METHOD_TYPE_AUTOMATIC),
                (string)$request->post('content', ''),
                (int)$request->post('isPop', SystemNotifyCenterModel::IS_POPUP_ON),
                (int)$request->post('popArea', 1),
                (string)$request->post('logTitle', '创建消息'),
            );
        } catch (\Exception $exception) {
            \ExceptionNotifier::report($exception);
            return $this->fail(500, $exception->getMessage());
        }
        return $this->success();
    }

    public function getUserNotifyList(Request $request)
    {
        $userId = $request->get('userId');
        $shopId = $request->get('shopId');

        $this->validate($request, [
            'userId' => 'required|integer',
            'shopId' => 'required|integer',
        ]);

        return $this->success(SystemNotifyService::getUserNotifyList($userId, $shopId));
    }
}
